package com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.tanzania.R;

@SuppressLint("NonConstantResourceId")
public class ZHITopTabBarItem extends RelativeLayout {

    private final Context mContext;
    private String tabTitle;
    private boolean checked;

    private TextView tv_main_title;

    private ZHITabBarItemClickListener mZHITabBarItemClickListener;

    public ZHITopTabBarItem(Context context) {
        this(context, null);
    }

    public ZHITopTabBarItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHITopTabBarItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZHITopTabBarItem);
        tabTitle = typedArray.getString(R.styleable.ZHITopTabBarItem_tabTitle);
        checked = typedArray.getBoolean(R.styleable.ZHITopTabBarItem_isChecked, false);
        typedArray.recycle();

        initView();
    }

    private void initView() {
        View mView = View.inflate(mContext, R.layout.view_top_tabbar_item, this);
        tv_main_title = mView.findViewById(R.id.tv_main_title);

        tv_main_title.setText(tabTitle);
        setChecked(checked);

        tv_main_title.setOnClickListener(v -> {
            setChecked(true);
            try {
                if (mZHITabBarItemClickListener != null) {
                    String gTag = (String) getTag();
                    if (!TextUtils.isDigitsOnly(gTag)) {
                        gTag = "0";
                    }
                    mZHITabBarItemClickListener.onTabClick(Integer.parseInt(gTag));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public void setChecked(boolean checked) {
        this.checked = checked;

        if (checked) {
            tv_main_title.setTextColor(getResources().getColor(R.color.theme_color_0));
            tv_main_title.setBackgroundColor(getResources().getColor(R.color.white));
            tv_main_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            tv_main_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            tv_main_title.setTextColor(getResources().getColor(R.color.white));
            tv_main_title.setBackgroundColor(getResources().getColor(R.color.theme_color_0));
            tv_main_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            tv_main_title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
    }

    public boolean isChecked() {
        return this.checked;
    }

    public void setZHITabBarItemClickListener(ZHITabBarItemClickListener listener) {
        this.mZHITabBarItemClickListener = listener;
    }
}
